package cn.zz.veggie.model.query;

import java.util.Collection;
import java.util.Objects;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.zz.comm.core.types.query.PageQuery;
import cn.zz.veggie.model.domain.OrderInfo;
import lombok.Getter;
import lombok.Setter;

/**
 * 订单明细表(OrderItem)查询器
 *
 * @author zhongq
 * @since 2024-05-25 16:18:54
 */
@Getter
@Setter
public class OrderInfoQuery {
	public Integer id;
	public Collection<Integer> ids;
	public Integer orderId;
	public Collection<Integer> orderIds;

	public static OrderInfoQuery ofOrderId(Integer orderId) {
		final OrderInfoQuery query = new OrderInfoQuery();
		query.orderId = orderId;
		return query;
	}

	public static OrderInfoQuery ofOrderIds(Collection<Integer> orderIds) {
		final OrderInfoQuery query = new OrderInfoQuery();
		query.orderIds = orderIds;
		return query;
	}

	public LambdaQueryWrapper<OrderInfo> toQueryWrapper() {
		final LambdaQueryWrapper<OrderInfo> queryWrapper = Wrappers.lambdaQuery();
		queryWrapper.eq(Objects.nonNull(id), OrderInfo::getId, id);
		queryWrapper.in(Objects.nonNull(ids), OrderInfo::getId, ids);
		queryWrapper.eq(Objects.nonNull(orderId), OrderInfo::getOrderId, orderId);
		queryWrapper.in(Objects.nonNull(orderIds), OrderInfo::getOrderId, orderIds);
		// default order by
		queryWrapper.orderByDesc(OrderInfo::getId);
		return queryWrapper;
	}

	// todo
	private void inject() {

	}

	public Page<OrderInfo> toPage(PageQuery p) {
		Page<OrderInfo> page = Page.of(p.getCurrent(), p.getSize());
		page.setOptimizeCountSql(p.isOptimizeCountSql());
		page.setSearchCount(p.isSearchCount());
		page.setOptimizeJoinOfCountSql(p.isOptimizeJoinOfCountSql());
		page.setCountId(p.getCountId());
		return page;
	}
}